package cn.sccl.dirms.dev.controller;

import cn.sccl.common.model.PageResult;
import cn.sccl.common.web.BaseController;
import cn.sccl.dirms.dev.model.DataSource;
import cn.sccl.dirms.dev.service.DataSourceManager;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 的Controller
 * Created by liupan on 2017-06-25.
 */
@RestController
@RequestMapping(value = "/dirms/dev/dataSource")
public class DataSourceController extends BaseController {

    @Autowired
    private DataSourceManager dataSourceManager;

    /**
     * 显示所有数据
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<DataSource> listAllDataSource() {
        return dataSourceManager.listAll();
    }


    @RequestMapping(value = "/find/classify/{classify}", method = RequestMethod.GET)
    public List<DataSource> queryDataByClassify(@PathVariable("classify") String classify) {
        return dataSourceManager.queryDataByClassify(classify);
    }


    /**
     * 分页查询，分页参数放置在url路径中
     *
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param request
     * @return
     */
    @RequestMapping(value = "/page/{pageIndex}/{pageSize}", method = RequestMethod.GET)
    public PageResult<DataSource> pageQueryDataSource(@PathVariable("pageIndex") long pageIndex, @PathVariable("pageSize") int pageSize, HttpServletRequest request) {
        return dataSourceManager.pageQueryByLike(pageIndex, pageSize, getQueryData(request));
    }

    /**
     * 分页查询，分页参数放置在request中
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public PageResult<DataSource> pageQueryDataSource(HttpServletRequest request) {
        return dataSourceManager.pageQueryByLike(getPageIndex(request), getPageSize(request), getQueryData(request));
    }

    /**
     * 保存数据
     *
     * @param dataSource
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public DataSource saveDataSource(@RequestBody DataSource dataSource) {
        dataSourceManager.save(dataSource);
        return dataSource;
    }

    /**
     * 根据ID删除数据
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @JsonView(Object.class)
    public void deleteDataSource(@PathVariable("id") String id) {
        dataSourceManager.delete(id);
    }

    /**
     * 测试数据连接
     *
     * @param dataSource
     * @return
     */
    @RequestMapping(value = "/connect", method = RequestMethod.POST)
    public JSONArray connect(@RequestBody DataSource dataSource) throws Exception {
        return dataSourceManager.connect(dataSource, JSONObject.parseObject(dataSource.getValType()));
    }

    /**
     * 获取数据
     *
     * @param key
     * @return
     */

    @RequestMapping(value = "/viewData", method = RequestMethod.POST )
    public JSONArray viewData(@RequestParam String key,@RequestBody JSONObject params) throws Exception {
        return dataSourceManager.viewData(key,params);
    }

    @RequestMapping(value = "/getSeriesExample", method = RequestMethod.GET )
    public JSONArray getSeriesExample(@RequestParam String key) throws Exception {
        JSONArray array = new JSONArray();
        for(int i=0;i<24;i++){
            JSONObject obj = new JSONObject();
            obj.put("clock",i+":00");
            obj.put("max-value", Math.random());
            obj.put("avg-value",Math.random());
            obj.put("min-value",Math.random());
            array.add(obj);
        }
        return  array;
    }


}
